﻿<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>【REST2SQL】10 REST2SQL操作指南</title>
  <link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>

<body class="stackedit">
  <div class="stackedit__html"><p><a href="https://blog.csdn.net/html5builder/article/details/135245013#comments_30761920">【REST2SQL】01RDB关系型数据库REST初设计</a><br>
<a href="https://blog.csdn.net/html5builder/article/details/135365560?spm=1001.2014.3001.5502">【REST2SQL】02 GO连接Oracle数据库</a><br>
<a href="https://blog.csdn.net/html5builder/article/details/135366380?spm=1001.2014.3001.5502">【REST2SQL】03 GO读取JSON文件</a><br>
<a href="https://blog.csdn.net/html5builder/article/details/135367263?spm=1001.2014.3001.5502">【REST2SQL】04 REST2SQL第一版Oracle版实现</a><br>
<a href="https://blog.csdn.net/html5builder/article/details/135451371?spm=1001.2014.3001.5502">【REST2SQL】05 GO 操作 达梦 数据库</a><br>
<a href="https://blog.csdn.net/html5builder/article/details/135460029?spm=1001.2014.3001.5502">【REST2SQL】06 GO 跨包接口重构代码</a><br>
<a href="https://blog.csdn.net/html5builder/article/details/135503604?spm=1001.2014.3001.5501">【REST2SQL】07 GO 操作 Mysql 数据库</a><br>
<a href="https://blog.csdn.net/html5builder/article/details/135527082?spm=1001.2014.3001.5502">【REST2SQL】08 日志重构增加输出到文件log.txt</a><br>
<a href="https://blog.csdn.net/html5builder/article/details/135530084?spm=1001.2014.3001.5501">【REST2SQL】09 给Go的可执行文件exe加图标和版本信息等</a></p>
<blockquote>
<p>【REST2SQL】从设计到现学Go到实现，大约一个多月的时间，为了致敬费曼学习法（通过自己的语言，用最简单的话把一件事向别人讲清楚，并且让外行人也能听懂。）这一段时间输出了不少博文，主要目的是自己的学习过程记录一下。年龄大了脑子记不住了，需要的时候再回头看看。</p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/direct/76df7ff351474526a7e1192e9bc0191e.png" alt="在这里插入图片描述"></p>
<p>REST2SQL核心功能就是连接数据库即可提供REST和SQL的CRUD服务。在config.json里设置数据库连接字符串及服务端口等参数，REST服务提供POST、GET、PUT、DELETE4种请求，SQL服务可执行INSERT、SELECT、UPDATE、DELETE4种SQL语句；请求结果返回JSON字符串（以后加上JSON SCHEMA）。开启REST2SQL服务，后台数据服务基本能覆盖到了，可以作为后端服务单独部署。<br>
REST2SQL可以作为内部开发的运维工具，也可以对外提供REST服务，后端开发再也没有CRUD重复开发的烦恼。</p>
<h1><a id="0__17"></a>0 我的开发测试环境</h1>

<table>
<thead>
<tr>
<th>项目</th>
<th>版本</th>
</tr>
</thead>
<tbody>
<tr>
<td>操作系统</td>
<td>Windows 10 家庭中文版，4 位操作系统, 基于 x64 的处理器</td>
</tr>
<tr>
<td>编程语言</td>
<td>Go 1.21.5</td>
</tr>
<tr>
<td>Oracle</td>
<td>Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production</td>
</tr>
<tr>
<td>DM</td>
<td>DM Database Server 64 V8 DB Version: 0x7000c</td>
</tr>
<tr>
<td>MySQL</td>
<td>8.0.35	MySQL Community Server - GPL	x86_6	Win64</td>
</tr>
<tr>
<td>浏览器</td>
<td>Microsoft Edge 120.0.2210.133 (正式版本) (64 位)</td>
</tr>
<tr>
<td>Curl</td>
<td>8.0.1 (Windows) libcurl/8.0.1 Schannel WinIDN Release-Date: 2023-03-20</td>
</tr>
<tr>
<td>Postman</td>
<td>December 2023 (v10.21)</td>
</tr>
</tbody>
</table><h1><a id="1_REST2SQL_28"></a>1 REST2SQL文件</h1>
<p>REST2SQL采用Go语言开发编译，只有4个文件，一个配置文件config.json,一个可执行文件rest2sql.exe，另外两个为操作指南。<br>
<img src="https://img-blog.csdnimg.cn/direct/2692fb03d5c14d6697a532dd22983637.png" alt="在这里插入图片描述"></p>
<h1><a id="2_REST2SQLconfigjson_32"></a>2 REST2SQL配置config.json</h1>
<p>用记事本打开config.json可配置REST2SQL,最重要是的数据库连接字符串connString。文件内容如下：</p>
<pre><code class="prism language-json"><span class="token punctuation">{</span>
	<span class="token string-property property">"hostPort"</span><span class="token operator">:</span><span class="token string">"localhost:5217"</span><span class="token punctuation">,</span>

	<span class="token string-property property">"connString"</span><span class="token operator">:</span><span class="token string">"mysql://blma:5217@(127.0.0.1:3306)/blma"</span><span class="token punctuation">,</span>

	<span class="token string-property property">"_oracle"</span><span class="token operator">:</span><span class="token string">"oracle://blma:5217@127.0.0.1:1521/CQYH"</span><span class="token punctuation">,</span>
	<span class="token string-property property">"_dm"</span><span class="token operator">:</span><span class="token string">"dm://BLMA:dameng5217@127.0.0.1:5236/BLMA"</span><span class="token punctuation">,</span>
	<span class="token string-property property">"_mysql"</span><span class="token operator">:</span><span class="token string">"mysql://blma:5217@(127.0.0.1:3306)/blma"</span><span class="token punctuation">,</span>
	<span class="token string-property property">"_mssql"</span><span class="token operator">:</span><span class="token string">"mssql://"</span><span class="token punctuation">,</span>
	<span class="token string-property property">"_postgresql"</span><span class="token operator">:</span><span class="token string">"postgresql://"</span><span class="token punctuation">,</span>
	
	<span class="token string-property property">"REST"</span><span class="token operator">:</span><span class="token string">"GET,POST,PUT,DELETE"</span><span class="token punctuation">,</span> 
	<span class="token string-property property">"_REST"</span><span class="token operator">:</span><span class="token string">"GET,POST,PUT,DELETE"</span><span class="token punctuation">,</span> 

	<span class="token string-property property">"SQL"</span><span class="token operator">:</span><span class="token string">"SELECT"</span><span class="token punctuation">,</span>
	<span class="token string-property property">"_SQL"</span><span class="token operator">:</span><span class="token string">"SELECT,INSERT,UPDATE,DELETE"</span>
	
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>hostPort 配置服务监听端口。</li>
<li>connString 配置数据库连接字符串，下面有_oracle、_dm、_mysql的配置范例，目前支持这三种数据库，以后还会扩展。</li>
<li>REST 配置可允许的REST请求，可在GET、POST、PUT、DELETE中选配，默认全部。</li>
<li>SQL 配置可允许执行的SQL语句，可在SELECT、INSERT、UPDATE、DELETE中选配，默认SELECT</li>
</ul>
<h1><a id="3_REST2SQL_60"></a>3 启动REST2SQL服务</h1>
<p>Windows系统下双击rest2sql.exe即可启动服务，启动成功后显示如下信息：<br>
<img src="https://img-blog.csdnimg.cn/direct/c933586d2d114623b18d825d8bb95839.png" alt="在这里插入图片描述"><br>
第一行为当前启动的配置信息，第二行为监听启动成功。</p>
<pre><code class="prism language-go"><span class="token number">2024</span><span class="token operator">/</span><span class="token number">01</span><span class="token operator">/</span><span class="token number">12</span> <span class="token number">10</span><span class="token punctuation">:</span><span class="token number">57</span><span class="token punctuation">:</span><span class="token number">32</span> config<span class="token punctuation">:</span><span class="token punctuation">{</span>mysql mysql<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>blma<span class="token punctuation">:</span><span class="token number">5217</span>@<span class="token punctuation">(</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">3306</span><span class="token punctuation">)</span><span class="token operator">/</span>blma localhost<span class="token punctuation">:</span><span class="token number">5217</span> GET<span class="token punctuation">,</span>POST<span class="token punctuation">,</span>PUT<span class="token punctuation">,</span>DELETE SELECT<span class="token punctuation">}</span>
<span class="token number">2024</span><span class="token operator">/</span><span class="token number">01</span><span class="token operator">/</span><span class="token number">12</span> <span class="token number">10</span><span class="token punctuation">:</span><span class="token number">57</span><span class="token punctuation">:</span><span class="token number">32</span> Starting Http Server at localhost<span class="token punctuation">:</span><span class="token number">5217</span>
</code></pre>
<p>关闭此控制台窗口就停止提供服务。</p>
<h1><a id="4_REST_71"></a>4 REST请求操作</h1>
<p>config.json文件里，配置 REST项，可以开启REST服务请求。只支持GET、POST、PUT、DELETE请求，可以设置0-4个请求，0个就是关闭REST请求。</p>
<pre><code class="prism language-json">	<span class="token string-property property">"REST"</span><span class="token operator">:</span><span class="token string">"GET,POST,PUT,DELETE"</span><span class="token punctuation">,</span> 
	<span class="token string-property property">"_REST"</span><span class="token operator">:</span><span class="token string">"GET,POST,PUT,DELETE"</span><span class="token punctuation">,</span> 
</code></pre>
<h2><a id="41_GET_78"></a>4.1 GET请求</h2>
<p>get请求支持where和orderby查询条件。</p>
<p><strong>1.curl请求</strong></p>
<pre><code class="prism language-go">curl http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>rest<span class="token operator">/</span>kezz
curl http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>rest<span class="token operator">/</span>kezz<span class="token operator">/</span>?where<span class="token operator">=</span>n_zzxj<span class="token operator">%</span><span class="token number">3E300</span>
curl http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>rest<span class="token operator">/</span>kezz<span class="token operator">/</span>?orderby<span class="token operator">=</span>p_id
curl http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>rest<span class="token operator">/</span>kezz<span class="token operator">/</span>?where<span class="token operator">=</span>n_zzxj<span class="token operator">%</span><span class="token number">3E300</span><span class="token operator">&amp;</span>orderby<span class="token operator">=</span>p_id
curl http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>rest<span class="token operator">/</span>kezz<span class="token operator">/</span>?orderby<span class="token operator">=</span>p_id<span class="token operator">&amp;</span>where<span class="token operator">=</span>n_zzxj<span class="token operator">%</span><span class="token number">3E300</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/direct/b816a15d598a4ddf9233633dc04f9e1a.png" alt="在这里插入图片描述"></p>
<p><strong>2. edge浏览器请求</strong></p>
<p><img src="https://img-blog.csdnimg.cn/direct/fa6ac610abff4c8bb1e8bc9565939e4a.png" alt="在这里插入图片描述"></p>
<p><strong>3.Postman请求</strong><br>
<img src="https://img-blog.csdnimg.cn/direct/72175cfcb6014c488bc92cdaa6ff3bf4.png" alt="在这里插入图片描述"></p>
<h2><a id="42_POST_99"></a>4.2 POST请求</h2>
<p>POST请求，url定位要请求的表，在加上data即可。</p>
<p><strong>1.Postman请求</strong><br>
<img src="https://img-blog.csdnimg.cn/direct/ad2b6a2f589449b683b09659df2c8a80.png" alt="在这里插入图片描述"></p>
<p><strong>2.curl请求</strong><br>
注意windows 10的curl 双引号""需要转义\</p>
<pre><code class="prism language-go">curl  <span class="token operator">-</span>X POST http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>rest<span class="token operator">/</span>kezz  <span class="token operator">--</span>data <span class="token punctuation">{</span>\"p_id\"<span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">300</span><span class="token punctuation">,</span>\"s_dm\"<span class="token punctuation">:</span>\"<span class="token number">200300</span>\"<span class="token punctuation">,</span>\"s_mc\"<span class="token punctuation">:</span>\"REST3\"<span class="token punctuation">}</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/direct/f4bc0884124b4af1bdff82edabbcb720.png" alt="在这里插入图片描述"></p>
<h2><a id="43_PUT_114"></a>4.3 PUT请求</h2>
<p>url加where定位要更新的记录。<br>
<strong>1.Postman请求</strong><br>
<img src="https://img-blog.csdnimg.cn/direct/66869efc35234d86820a239a0f26a9de.png" alt="在这里插入图片描述"></p>
<p><strong>2.curl请求</strong></p>
<pre><code class="prism language-go">curl  <span class="token operator">-</span>X PUT http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>rest<span class="token operator">/</span>kezz<span class="token operator">/</span>?where<span class="token operator">=</span>p_id<span class="token operator">=</span><span class="token operator">-</span><span class="token number">200</span>  <span class="token operator">--</span>data <span class="token punctuation">{</span>\"s_mc\"<span class="token punctuation">:</span>\"CURL\"<span class="token punctuation">}</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/direct/4e6bccdb50ba46deb521a4ce824b5206.png" alt="在这里插入图片描述"></p>
<h2><a id="44_DELETE_127"></a>4.4 DELETE请求</h2>
<p>url加where定位要删除的记录。<br>
<strong>1.Postman请求</strong><br>
<img src="https://img-blog.csdnimg.cn/direct/c52b8cbe3988472fb3b53dc52b56bd3a.png" alt="在这里插入图片描述"></p>
<p><strong>2.curl请求</strong></p>
<pre><code class="prism language-go">curl  <span class="token operator">-</span>X DELETE http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>rest<span class="token operator">/</span>kezz<span class="token operator">/</span>?where<span class="token operator">=</span>p_id<span class="token operator">=</span><span class="token operator">-</span><span class="token number">200</span> 
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/direct/1eff5ac7c33c46b8a8aefd3381b271a0.png" alt="在这里插入图片描述"></p>
<h1><a id="5_SQL_138"></a>5 SQL执行操作</h1>
<p>config.json文件里，配置 SQL项，可以开启SQL服务请求。只支持SELECT,INSERT,UPDATE,DELETE请求，可以设置0-4个请求，0个就是关闭SQL请求。<br>
默认设置只能执行SELECT。<br>
URL栏可以直接输入SQL即可执行。</p>
<h2><a id="51_SELECT_142"></a>5.1 执行SELECT语句</h2>
<pre><code class="prism language-go">http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>sql<span class="token operator">/</span><span class="token keyword">select</span> <span class="token operator">*</span> from kezz
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/direct/85fa8605736b489084658e99aed9e7bd.png" alt="在这里插入图片描述"></p>
<h2><a id="52_INSERT_150"></a>5.2 执行INSERT语句</h2>
<pre><code class="prism language-go">http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>sql<span class="token operator">/</span>insert into kezz <span class="token punctuation">(</span>p_id<span class="token punctuation">,</span>s_dm<span class="token punctuation">,</span>s_mc<span class="token punctuation">)</span> values <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">400</span><span class="token punctuation">,</span><span class="token char">'400400'</span><span class="token punctuation">,</span><span class="token char">'SQL'</span><span class="token punctuation">)</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/direct/b41db0b7a7dd4769a8b9dec7f1d1567a.png" alt="在这里插入图片描述"></p>
<h2><a id="53_UPDATE_156"></a>5.3 执行UPDATE语句</h2>
<pre><code class="prism language-go">http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>sql<span class="token operator">/</span>update<span class="token operator">%</span>20kezz<span class="token operator">%</span>20set<span class="token operator">%</span>20s_mc<span class="token operator">=</span><span class="token char">'update'</span><span class="token operator">%</span>20where<span class="token operator">%</span>20p_id<span class="token operator">%</span><span class="token number">20</span><span class="token operator">=</span><span class="token operator">%</span><span class="token number">20</span><span class="token operator">-</span><span class="token number">400</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/direct/5aca50bb07e24135ad3c89f9443c1ae4.png" alt="在这里插入图片描述"></p>
<h2><a id="54_DELETE_163"></a>5.4 执行DELETE语句</h2>
<pre><code class="prism language-go">http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">5217</span><span class="token operator">/</span>sql<span class="token operator">/</span><span class="token builtin">delete</span><span class="token operator">%</span>20from<span class="token operator">%</span>20kezz<span class="token operator">%</span>20where<span class="token operator">%</span>20p_id<span class="token operator">=</span><span class="token operator">-</span><span class="token number">400</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/direct/c0d8f2a7d4bd4465ab05f0e12964c375.png" alt="在这里插入图片描述"></p>
<h1><a id="6__169"></a>6 资源下载</h1>
<p><a href="https://download.csdn.net/download/sufuq/88684853">REST2SQL实现连接数据库，数据库的表或视图即可提供REST的GET\POST\PUT\DELETE请求，SQL可执行SQL</a><br>
https://download.csdn.net/download/sufuq/88684853</p>
</div>
</body>

</html>
